package com.cloudcc.mobile.view.schedule;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Toast;

import com.cloudcc.cloudframe.adapter.ViewHolder;
import com.cloudcc.mobile.ExtraConstant;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.adapter.CoworkerListAdapter;
import com.cloudcc.mobile.entity.CoworkerEntity;
import com.cloudcc.mobile.event.CoworkerEventList.CoworkerListEvent;
import com.cloudcc.mobile.util.UrlTools;
import com.cloudcc.mobile.util.UserUtils;
import com.cloudcc.mobile.view.main.fragment.CoworkerListFragment;

/** 
* @Description: 查看下属 
* @author wangzhiqiang
* @date 2015-12-17 下午2:35:03 
*  
*/
public class SubStaffListFragment  extends CoworkerListFragment{

	@Override
	public void init() {
		super.init();
		mLoadMoreLayout.setEnabled(false);
		headerbar.setLeftImageResource(R.drawable.ease_mm_title_back);
		headerbar.setTitle("我的下属");
		headerbar.setRightText("");
	}
	@Override
	protected void initRequestData() {
		headerbar.beginProgress();
		requestData();
	}
	@Override
	public void requestData() {
		headerbar.beginProgress();
		mContactPresenter.getSubStaffList();
	}
	
	
	public void onEventMainThread(CoworkerListEvent event){
		headerbar.completeProgress();
		mRefreshLayout.refreshComplete();
		if (event.isError()) {
			Toast.makeText(mContext, event.getMessage(), 0).show();
			return;
		}
		refreshData(event.getData());
	}
	
	protected void refreshData(List<CoworkerEntity> data){
		if (mAdapter == null) {
			mAdapter = new MySubListAdapter(mContext);
			mListView.setAdapter(mAdapter);
			mListView.setOnItemClickListener(this);
		}
		if (isFromRefresh()) {
			mContactes.clear();
			mContactes.addAll(data);
			mAdapter.changeData(data);
		} else {
			mContactes.addAll(data);
			mAdapter.addData(data);
		}
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
		Intent intent = new Intent(getActivity(),
				ScheduleMainActivity.class);
		CoworkerEntity item = mAdapter.getItem(position);
		intent.putExtra(ExtraConstant.EXTRA_ID, item.getId());
		intent.putExtra(ExtraConstant.EXTRA_NAME, item.getName());
		startActivity(intent);	
		getActivity().finish();
	}
	private class MySubListAdapter extends CoworkerListAdapter{

		public MySubListAdapter(Context mContext) {
			super(mContext);
		}
		
		@Override
		public void convert(ViewHolder holder, CoworkerEntity t) {
			ImageView ivLogo = holder.getView(R.id.contact_item_name);
			String uri = UrlTools.getTopImage(t.getId());
			int colorindex=holder.getPostion()%colors.length;
			UserUtils.setLogoRound(uri, ivLogo, colors[colorindex], t.getNickRowName(), mContext);
			holder.setText(R.id.contact_item_logo, t.getName());
		}
		
		@Override
		public int getLayoutId() {
			return R.layout.tongshi_list_sub;
		}
		
	}
	@Override
	public void onClickLeft(View v) {
		getActivity().finish();
	}
	@Override
	public void onClickRight(View v) {
	}
	
	
}
